/**
 * 失联人员信息
 */
var orgData = _appConfigs.getUserData().userOrgs;
$(function(){	
	$("#loseTouch_sex").combobox({
		data:$.appCache.get("dict_gender"),
		valueField:'code',    
	    textField:'name',
	    editable:false,
	    panelMaxHeight:90    
	});
	
	$("#losetouchtime").combobox({
		data:$.appCache.get("dict_losetouchtime"),
		valueField:'code',    
	    textField:'name',
	    editable:false,
	    panelMaxHeight:90    
	});
	
	$("#loseTouch_community").combobox({
		data:$.appCache.get("dict_community"),
		valueField:'code',    
	    textField:'name',
	    required:true,
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90    
	});
	
	$("#loseTouch_street").combobox({
		data:$.appCache.get("dict_street"),
		valueField:'code',    
	    textField:'name',
	    required:true,
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90,
	    onSelect: function(record){
	    	$("#loseTouch_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    required:true,
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	
	$("#loseTouchSearch_street").combobox({
		data:$.appCache.get("dict_street"),
		valueField:'code',    
	    textField:'name',
	    editable:false,
	    panelMaxHeight:90,
	    onSelect: function(record){
	    	$("#loseTouchSearch_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	
});
/**
 * 默认选街道
 */
if(orgData.porgId == _appConfigs.appOrgId){
	$('#loseTouchSearch_street').combobox({disabled:true});
	$('#loseTouchSearch_street').combobox('select',orgData.orgId);
	$('#loseTouch_street').combobox({readonly:true});
	$('#loseTouch_street').combobox('select',orgData.orgId);
}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){
	$('#loseTouchSearch_street').combobox({disabled:true});
	$('#loseTouchSearch_community').combobox({disabled:true});
	$('#loseTouchSearch_street').combobox('select',orgData.porgId);
	$('#loseTouchSearch_community').combobox('select',orgData.orgId);
	$('#loseTouch_street').combobox({readonly:true});
	$('#loseTouch_community').combobox({readonly:true});
	$('#loseTouch_street').combobox('select',orgData.porgId);
	$('#loseTouch_community').combobox('select',orgData.orgId);
}else{
	$("#loseTouchSearch_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90
	}); 
	$("#loseTouch_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90
	}); 
	$("#loseTouch_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90
	}); 
}

$('#loseTouchInfoGrid').datagrid({
	url:'labor/management/getLoseTouchInfoList',  
	queryParams:{
		street: $('#loseTouchSearch_street').combobox('getValue'),
		community: $('#loseTouchSearch_community').combobox('getValue')
	},
	idField:'laborinfoId',
    rownumbers:true,
    fitColumns:true,
    singleSelect:true,
    pagination:true,
    columns:[[ 
        {field:'laborinfoId',title:'主键',hidden:'ture'},      
        {field:'name',title:'姓名',width:90,align:'center'},    
        {field:'sex',title:'性别',width:80,align:'center',
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_gender');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},   
        {field:'idcard',title:'身份证号',width:125,align:'center'},   
        {field:'residenceAddress',title:'户籍地址',width:150,align:'center'},    
        {field:'losetouchtime',title:'失联时间',width:100,align:'center',
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_losetouchtime');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},   
        {field:'recentdate',title:'最后联系时间',width:100,align:'center',
        	   formatter:function(data){
        		   if(data!=null){
        			   var df = new Date(data);
                	   return df.format("yyyy-MM-dd");  
            	   } else{
            		   return null;      		        			   
            	   }
            }},
        {field:'contactperson',title:'联系人',width:90,align:'center'}, 
        {field:'street',title:'街道',width:100,align:'center',
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_street');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},   
        {field:'community',title:'社区',width:100,align:'center',
               formatter:function(value){
            	   var dict = $.appCache.get('dict_community');
                   if(!dict || !dict.length) {
                	   return value;
                   } else { 
                	   var i = 0;
                	   for(i; i<dict.length; i++) {
                		   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},   
        {field:'remark',title:'备注',width:90, align:'center'}
    ]]
    
}); 


/**
 * 查询条件
 */
function loseTouchSearch(){
	$('#loseTouchInfoGrid').datagrid('load',{
		street: $('#loseTouchSearch_street').combobox('getValue'),
		community: $('#loseTouchSearch_community').combobox('getValue'),
		name: $('#loseTouchSearch_name').val(),	
		idcard: $('#loseTouchSearch_idCard').val() 					
	});
	$('#loseTouchInfoGrid').datagrid('unselectAll');
}

/**
 * 回显失联人员信息
 */
function getLoseTouchInfo(){
	var rowData = $('#loseTouchInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$('#modifyLoseTouchInfo').window('open');
		$.ajax({
				url:'labor/management/getLoseTouchInfo',
				type:'post',
				data:{
					//根据id值查询该条记录
					laborinfoId:rowData.laborinfoId
				},
				success: function (data) { 
					if(data==null){
						$('#modifyLoseTouchInfoForm').form('clear');
					}else{
						$('#modifyLoseTouchInfoForm').form('load',data);
					}
		        },
			});
		}
}

/**
 * 修改失联人员信息
 * 
 */
function modifyLoseTouchInfo(){
	$('#modifyLoseTouchInfoForm').form('submit',{
		url:'labor/management/updateLoseTouchInfo',
		success: function(data){
			data =  $.parseJSON(data);
			if(data.success == 'true'){
				$.messager.alert('提示','修改成功','info');
				$('#modifyLoseTouchInfo').window('close');
				$('#modifyLoseTouchInfoForm').form('clear');
				loseTouchSearch();
			}else{
				$.messager.alert('提示',data.message,'info');
			}
		}

	});
}

function exportLosTouchInfo(){
	 var street,community;
	 if(orgData.porgId == _appConfigs.appOrgId){  //街道
		 street = orgData.orgId;
		 community = "";
	 }else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){ //社区
		 street = "";
		 community = orgData.orgId;
	 }else{  //全部
		 street = "";
		 community = "";
	 }
	 window.location.href ="labor/excel/exportLoseTouchInfo?street="+street+"&community="+community;
}
